﻿using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour {
	
	private GameObject player1;
	private GameObject player2;
	
	public float zooming = 3;
	public float zoomMin = 2;
	public float zoomMax = 5;
	public float viewHeight = 24.0f;
	public float borderRatio = 0.2f;
	public float verticalOffset = 0.0f;
	
	private Camera camera;
	
	// Use this for initialization
	void Start () {
		camera = GetComponent<Camera> ();

		player1 = GameSystem.mastermind;
		player2 = GameSystem.plateformer;
	}
	
	// Update is called once per frame
	void Update () {
		float height = 2f * camera.orthographicSize;
		float width = height * camera.aspect;
		float dist = Mathf.Abs(player1.transform.position.x - player2.transform.position.x) * (1.0f + borderRatio);
		camera.orthographicSize = Mathf.Max((dist / width) * camera.orthographicSize, viewHeight * 0.5f);
		transform.position = new Vector3((player1.transform.position.x + player2.transform.position.x) / 2, viewHeight * 0.5f + verticalOffset, transform.position.z);
	}
}
